﻿@model IEnumerable<Von.Web.Structs.TicketNoInfo>
@{
    ViewData["Title"] = "票证编码规则管理";
}
<h2><span id="appTitle"></span>票证编码规则管理<a class="btn btn-link" onclick="NewTicketNo()">新建票证编码规则</a></h2>
<table id="ruleList" class="table">
    <thead>
        <tr>
            <th>规则名称</th>
            <th>当前序号</th>
            <th>变更类型</th>
            <th>编码规则</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.RuleName)</td>
                <td>@Html.DisplayFor(modelItem => item.CurrentID)</td>
                <td>@{ var kindName = ""; switch (item.Kind) { case 0: kindName = "每年重置"; break; case 1: kindName = "每月重置"; break; case 2: kindName = "每日重置"; break; case 3: kindName = "每时重置"; break; } }  @kindName</td>
                <td>@Html.DisplayFor(modelItem => item.Rule)</td>
                <td>
                    <a class="btn-primary" onclick="EditTicketNo(this,'@item.RuleName')">编辑</a> |
                    <a class="btn-danger" onclick="DeleteTicketNo(this,'@item.RuleName')">删除</a>
                </td>
            </tr>
        }
    </tbody>
</table>
<!-- 模态框（Modal） -->
<div class="modal fade" id="editor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true" data-mode="new">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color:aquamarine;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="ModalLabel">票证编码规则编辑</h4>
                <input id="flag" type="hidden" />
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="RuleName" class="control-label">规则名称</label>
                    <input id="RuleName" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="CurrentID" class="control-label">当前序号</label>
                    <input id="CurrentID" type="number" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="Kind" class="control-label">变更类型 </label>
                    <select id="Kind" class="form-control">
                        <option value="0">每年重置</option>
                        <option value="1">每月重置</option>
                        <option value="2">每日重置</option>
                        <option value="3">每时重置</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="RuleDescription" class="control-label">规则描述</label>
                    <select id="RuleDescription" class="form-control" onchange="changeDesc()">
                        <option value="{2}{0}">四位年度及年度序号</option>
                        <option value="{1}{0}">两位年度及年度序号</option>
                        <option value="{2}{3}{0}">四位年两位月及月序号</option>
                        <option value="{1}{3}{0}">两位年两位月及月序号</option>
                        <option value="{2}{3}{4}{0}">四位年两位月两位日及日序号</option>
                        <option value="{1}{3}{4}{0}">两位年两位月两位日及日序号</option>
                        <option value="{2}{3}{4}{5}{0}">四位年两位月两位日两位时及时序号</option>
                        <option value="{1}{3}{4}{5}{0}">两位年两位月两位日两位时及时序号</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="NumLength" class="control-label">编码长度</label>
                    <input id="NumLength" class="form-control" onchange="changeDesc()" value="6" />
                </div>
                <div class="form-group">
                    <label for="Rule" class="control-label">编码规则</label>
                    <div class="input-group">
                        <input id="Rule" class="input-control" />
                        <input class="input-add-on" type="button" value="测试" onclick="getTicket()" />
                        <input id="ticketNo" class="input-add-on" />
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="flag" />
                <div class="form-group">
                    <input value="保存" class="btn btn-default" onclick="SaveTicketNo()" />
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</div>
<script type="text/javascript">
    function dataToHtml(data) {
        var kindName = "";
        switch (data.Kind) {
            case 0: kindName = "每年重置";
            case 1: kindName = "每月重置";
            case 2: kindName = "每日重置";
            case 3: kindName = "每时重置";
        }
        return "<tr><td>" + data.RuleName + "</td><td>" + data.CurrentID + "</td><td>" + kindName + "</td><td>" + data.Rule + "</td><td><a class=\"btn-primary\" onclick=\"EditTicketNo(this,'" + data.RuleName + "')\">编辑</a>|<a class=\"btn-danger\" onclick=\"DeleteTicketNo(this,'" + data.RuleName + "')\" > 删除</a></td></tr>";
    }
    function selected(d) {
        $("[data-rule-sel=true]").removeAttr("data-rule-sel");
        if (d != null) $(d).parent().parent().attr("data-rule-sel", true);
    }
    function changeDesc() {
        $("#Rule").val($("#RuleDescription").val().replace("{0}", "{0:D" + $("#NumLength").val() + "}"));
    }
    function getTicket() {
        var data = {
            RuleName : $("#RuleName").val(),
            CurrentID : $("#CurrentID").val(),
            Kind : $("#Kind").val(),
            NumLength : $("#NumLength").val(),
            Rule : $("#Rule").val()
        };
        AjaxPostData('@Url.Ajax("TestTicketNo")', data, function (result) { $("#ticketNo").val(result.Result) });
    }
    function NewTicketNo() {
        selected();
        $("#RuleName").val("");
        $("#CurrentID").val("");
        $("#Kind").val(0);
        $("#RuleDescription").val("");
        $("#Rule").val("");
        $("#flag").val("new");
        $('#editor').modal('show');
    }
    function EditTicketNo(d, id) {
        selected(d);
        AjaxPostData('@Url.Ajax("GetByTicketNo")', { 'RuleName': id }, function (info) {
            $("#RuleName").val(info.RuleName);
            $("#CurrentID").val(info.CurrentID);
            $("#Kind").val(info.Kind);
            $("#NumLength").val(info.NumLength);
            $("#Rule").val(info.Rule);
            $("#flag").val("edit");
            $('#editor').modal('show');
        });
    }
    function DeleteTicketNo(d, id) {
        if (confirm('确定删除吗？')) {
            selected(d);
            AjaxPostData('@Url.Ajax("DelTicketNo")', { 'RuleName': id },
                function (result) { if (result = true) $("[data-rule-sel=true]").remove(); else SysWarning(data); });
        }
    }
    function SaveTicketNo() {
        var data = {
            RuleName : $("#RuleName").val(),
            CurrentID : $("#CurrentID").val(),
            Kind : $("#Kind").val(),
            NumLength : $("#NumLength").val(),
            Rule : $("#Rule").val()
        };
        var url = $("#flag").val() =="edit" ? '@Url.Ajax("EditTicketNo")' : '@Url.Ajax("AddTicketNo")';
        AjaxPostData(url, data, function (result) {
            if ($("#flag").val() == "edit") $("[data-rule-sel=true]").replaceWith(dataToHtml(result));
            else $("#ruleList tbody").append(dataToHtml(result));
            $('#editor').modal('hide');
        });
    }
</script>

